package com.magist.manage.email.service.impl;

import com.magist.core.util.DateUtil;
import com.magist.core.util.PrintOutUtil;
import com.magist.core.util.SendEmailUtils;
import com.magist.manage.copyrights.entity.SupplyContract;
import com.magist.manage.email.service.EmailSender;
import com.magist.manage.enums.BalanceTypeEnum;
import com.magist.manage.enums.ContractTypeEnum;
import com.magist.manage.enums.ValidStatusEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Component
public class SupplyContractEmailSenderImpl implements EmailSender<SupplyContract> {



    @Override
    public void sendEmail(List<SupplyContract> datas, String subject, String to) {
        if (!CollectionUtils.isEmpty(datas)) {
            StringBuilder content = new StringBuilder(
                    "<table border=\"1\" cellspacing=\"0\">" +
                    "<caption><h1>" + subject +"</h1></caption>" +
                    "<thead><tr>" +
                    "<th>ID</th>" +
                    "<th>合同编号</th>" +
                    "<th>合同名称</th>" +
                    "<th>供应商简称</th>" +
                    "<th>到期状态</th>" +
                    "<th>协议有效期</th>" +
                    "<th>开始时间</th>" +
                    "<th>终止时间</th>" +
                    "<th>结算形式</th>" +
                    "<th>价格</th>" +
                    "<th>合同状态</th>" +
                    "<th>操作人</th>" +
                    "<th>修改时间</th>" +
                    "<th>操作</th>" +
                    "</tr></thead>");
            datas.forEach(supplyContract -> {

                Date now = new Date();
                Date endDate = supplyContract.getEndDate();
                String expireStatus = null;
                int monthDiff = DateUtil.getMonthDiff(endDate, now);
                if (monthDiff == 0) {
                    expireStatus = "1个月内到期";
                } else if (monthDiff > 0 && monthDiff < 3) {
                    expireStatus = "3个月内到期";
                } else if (monthDiff >= 3 && monthDiff < 6) {
                    expireStatus = "6个月内到期";
                } else if (monthDiff >= 6) {
                    expireStatus = "6个月以外到期";
                }
                if (now.after(endDate)) {
                    expireStatus = "已到期";
                }
                content.append("<tr><td>" + supplyContract.getShowId() + "</td>");
                content.append("<td>" + supplyContract.getContractNo() + "</td>");
                content.append("<td>" + supplyContract.getContractName() + "</td>");
                content.append("<td>" + supplyContract.getSupplyName() + "</td>");
                content.append("<td>" + expireStatus + "</td>");
                content.append("<td>" + supplyContract.getMounthNum() + "个月</td>");
                content.append("<td>" + DateUtil.format(supplyContract.getBeginDate(),DateUtil.yyyy_MM_dd) + "</td>");
                content.append("<td>" + DateUtil.format(supplyContract.getEndDate(),DateUtil.yyyy_MM_dd) + "</td>");
                content.append("<td>" + BalanceTypeEnum.getByCode(supplyContract.getBalanceType()).getDesc() + "</td>");
                content.append("<td>" + PrintOutUtil.printOut(supplyContract.getTotalAmount()) + "</td>");
                content.append("<td>" + ValidStatusEnum.getByCode(supplyContract.getContractStatus()).getDesc() + "</td>");
                content.append("<td>" +  PrintOutUtil.printOut(supplyContract.getModifier()) + "</td>");
                content.append("<td>" + DateUtil.format(supplyContract.getModifyTime(),DateUtil.yyyy_MM_dd_HH_mm_ss) + "</td>");
                content.append("<td><a href=\"http://kada.dumeiweiping.com/index.html#/supplyContract/" + (ContractTypeEnum.IN.getCode().equals(supplyContract.getContractType())?"editIn" : "editOut") +"?id=" + supplyContract.getId()+"\">" +  "去操作" + "</a></td></tr>");
                // http://kada.dumeiweiping.com/index.html#/supplyContract/editIn?id=18080200001019224
            });
            content.append("</table>");
            SendEmailUtils.sendEmail(subject, content.toString(), to);
        }


    }
}
